import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: () => import("@/view/Login"),
    meta: { title: "登录" },
  },
  {
    path: "/studenthome",
    redirect: "/studenthome/message",
    component: () => import("@/view/Student/Home"),
    children: [
      {
        path: "/studenthome/message",
        component: () => import("@/components/Student/Message/MessageList"),
        meta: { title: "消息列表" },
      },
      {
        path: "/studenthome/info",
        component: () => import("@/components/Student/PersonalData"),
        meta: { title: "个人信息" },
      },
      {
        path: "/studenthome/apply",
        component: () => import("@/components/Student/Apply/ApplyMain"),
        meta: { title: "实习申请" },
      },
      {
        path: "/studenthome/group",
        component: () => import("@/components/Student/Group/GroupMain"),
        meta: { title: "实习小组" },
      },
    ],
  },
  {
    path: "/teacherhome",
    redirect: "/teacherhome/message",
    component: () => import("@/view/Teacher/Home"),
    children: [
      {
        path: "/teacherhome/message",
        component: () => import("@/components/Teacher/Message/MessageList"),
        meta: { title: "消息列表" },
      },
      {
        path: "/teacherhome/info",
        component: () => import("@/components/Teacher/PersonalData"),
        meta: { title: "个人信息" },
      },
      {
        path: "/teacherhome/group",
        component: () => import("@/components/Teacher/Group/GroupMain"),
        meta: { title: "实习小组" },
      },
    ],
  },
  {
    path: "*",
    component: () => import("@/view/error"),
    meta: { title: "错误" },
  },
];
const router = new VueRouter({
  mode: "history",
  scrollBehavior: () => ({ y: 0 }),
  routes,
});
router.beforeEach((to, from, next) => {
  document.title = to.meta.title; //设置网页标题
  if (to.path == "/login") return next();
  const token = window.sessionStorage.getItem("accessToken");
  if (!token) {
    return next("/login"); //token不存在
  }
  next();
});
export default router;
